package com.ccrfid.rmc6.resource.sys;

import com.ccrfid.rmc6.appconfig.auth.Secured;
import com.ccrfid.rmc6.appconfig.exception.pojo.AppException;
import com.ccrfid.rmc6.appconfig.exception.pojo.CustomErrors;
import com.ccrfid.rmc6.entity.sys.AppModuleEntity;
import com.ccrfid.rmc6.entity.sys.RoleEntity;
import com.ccrfid.rmc6.pojo.param.PageParam;
import com.ccrfid.rmc6.repository.search.Criterion;
import com.ccrfid.rmc6.repository.search.SearchCriteria;
import com.ccrfid.rmc6.resource.auth.pojo.UserPrincipal;
import com.ccrfid.rmc6.service.sys.RoleService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;

import javax.ws.rs.*;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import javax.ws.rs.core.SecurityContext;
import java.util.List;

@Secured
@Path("roles")
@Produces(MediaType.APPLICATION_JSON)
public class RoleResource {
    private static final Logger logger = LoggerFactory.getLogger(RoleResource.class);
    @Context
    private SecurityContext securityContext;
    @Autowired
    private RoleService roleService;

    @GET
    @Path("search")
    public Page<RoleEntity> getRolePage(@QueryParam("roleName") String roleName, @BeanParam PageParam pageParam) {
        SearchCriteria criteria = SearchCriteria.Builder.create();
        if (roleName != null) {
            criteria.add(new Criterion("roleName", Criterion.Operator.LIKE, '%' + roleName + '%'));
        }
        PageRequest pageRequest;
        if (pageParam.getOrders() != null && pageParam.getOrders().size() > 0) {
            pageRequest = PageRequest.of(pageParam.getPageNumber(), pageParam.getPageSize(), Sort.by(pageParam.getOrders()));
        } else {
            pageRequest = PageRequest.of(pageParam.getPageNumber(), pageParam.getPageSize());
        }
        Page<RoleEntity> page = roleService.getRolePage(criteria, pageRequest);
        return page;
    }

    @GET
    public List<RoleEntity> getRoleList() {
        List<RoleEntity> list = roleService.getRoleList();
        return list;
    }

    @POST
    @Consumes(MediaType.APPLICATION_JSON)
    public RoleEntity createRole(RoleEntity entity) throws AppException {
        RoleEntity roleEntity = roleService.createRole(entity);
        return roleEntity;
    }

    @DELETE
    @Consumes(MediaType.APPLICATION_JSON)
    public int deleteRoles(List<Long> idList) throws AppException {
        if (idList == null) {
            throw new AppException(Response.Status.BAD_REQUEST.getStatusCode(),
                    CustomErrors.param_is_null.getCode(), CustomErrors.param_is_null.getReason(), "");
        }
        return roleService.deleteRoles(idList);
    }

    @GET
    @Path("{id}")
    public RoleEntity getRole(@PathParam("id") Long roleId) {
        RoleEntity roleEntity = roleService.getRole(roleId);
        return roleEntity;
    }

    @PUT
    @Path("{id}")
    @Consumes(MediaType.APPLICATION_JSON)
    public RoleEntity updateRole(@PathParam("id") Long roleId, RoleEntity entity) throws AppException {
        RoleEntity roleEntity = roleService.updateRole(roleId, entity);
        return roleEntity;
    }

    @GET
    @Path("{roleId}/app-modules")
    public List<AppModuleEntity> getAppModules(@PathParam("roleId") Long roleId) throws AppException {
        UserPrincipal userPrincipal = (UserPrincipal) securityContext.getUserPrincipal();
        return roleService.getAppModules(roleId, userPrincipal);
    }

    @PUT
    @Path("{roleId}/app-modules")
    @Consumes(MediaType.APPLICATION_JSON)
    public Boolean updateRoleModuleList(@PathParam("roleId") Long roleId, List<Long> moduleIdList) throws AppException {
        UserPrincipal userPrincipal = (UserPrincipal) securityContext.getUserPrincipal();
        roleService.updateRoleModuleList(roleId, userPrincipal, moduleIdList);
        return true;
    }
}
